<?php
namespace App\Http\Controllers;


use App\Http\Logic\TransferLogic;
use App\Http\Requests\Transfer\CreateTransferOrderRequest;
use App\Http\Requests\Transfer\OrderInfoRequest;
use App\Http\Requests\Transfer\PayOrderRequest;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class TransferController extends BaseController
{
    public function transferView() : JsonResponse
    {
        $user_id = auth() -> user() -> id;

        $res = TransferLogic::getViewData($user_id);

        if($res === false) {
            return $this->error(TransferLogic::getErrorMessage());
        }

        return $this -> success($res);
    }

    public function createTransferOrder(CreateTransferOrderRequest $request) : JsonResponse
    {
        $pay_type = $request -> input('pay_type');

        $num = $request -> input('num');

        $user_id = auth() -> user() -> id;

        $res = TransferLogic::createOrder($user_id, $pay_type, $num);

        if($res === false) {
            return $this->error(TransferLogic::getErrorMessage());
        }

        return $this->success(['order_id' => $res]);
    }

    public function cancelTransferOrder(Request $request) : JsonResponse
    {
        $id = $request -> input('id');

        $cancel_desc = $request -> input('cancel_desc');

        $res = TransferLogic::cancelOrder($id, $cancel_desc);

        if($res === false) {
            return $this->error(TransferLogic::getErrorMessage());
        }

        return $this->success();
    }

    public function getTransferOrderList(Request $request) : JsonResponse
    {
        $pay_type = $request -> input('pay_type') ?? "";

        $pageSize = $request -> input('page_size') ?? 15;

        $pageNo = $request -> input('page_no') ?? 1;

        $list = TransferLogic::orderList($pay_type, $pageSize, $pageNo);

        return $this->success($list);
    }

    public function getTransferOrderInfo(OrderInfoRequest $request) : JsonResponse
    {
        $id = $request -> input('id');

        $list = TransferLogic::orderInfo($id);

        return $this->success($list);
    }

    public function getTransferOrderBuyerInfo(Request $request) : JsonResponse
    {
        $id = $request -> input('buyer_order_id');

        $list = TransferLogic::buyerOrderInfo($id);

        return $this->success($list);
    }

    public function lockOrder(OrderInfoRequest $request) : JsonResponse
    {
        $id = $request -> input('id');

        $user_id = auth() -> user() -> id;

        $buyer_order_id = TransferLogic::lockOrder($id, $user_id);

        if($buyer_order_id === false) {
            return $this->error(TransferLogic::getErrorMessage());
        }

        return $this->success($buyer_order_id);
    }

    public function payOrder(PayOrderRequest $request) : JsonResponse
    {
        $buyer_order_id = $request -> input('buyer_order_id');

        $user_id = auth() -> user() -> id;

        $res = TransferLogic::payOrder($buyer_order_id, $user_id);

        if($res === false) {
            return $this->error(TransferLogic::getErrorMessage());
        }

        return $this->success();
    }

    public function confirmPayOrder(OrderInfoRequest $request) : JsonResponse
    {
        $id = $request -> input('id');

        $user_id = auth() -> user() -> id;

        $res = TransferLogic::confirmPayOrder($id, $user_id);

        if($res === false) {
            return $this->error(TransferLogic::getErrorMessage());
        }

        return $this->success();
    }


    public function cancelPayOrder(PayOrderRequest $request) : JsonResponse
    {
        $buyer_order_id = $request -> input('buyer_order_id');

        $cancel_desc = $request -> input('cancel_desc') ?? '其他原因';

        $res = TransferLogic::cancelPayOrder($buyer_order_id, $cancel_desc);

        if($res === false) {
            return $this->error(TransferLogic::getErrorMessage());
        }

        return $this->success();
    }

    public function getBuyerOrderList(Request $request) : JsonResponse
    {
        $pay_status = $request -> input('pay_status') ?? "";

        $pageSize = $request -> input('page_size') ?? 15;

        $pageNo = $request -> input('page_no') ?? 1;

        $user_id = auth() -> user() -> id;

        $list = TransferLogic::getBuyerOrderList($user_id, $pay_status, $pageSize, $pageNo);

        return $this->success($list);
    }

    public function getSellerOrderList(Request $request) : JsonResponse
    {
        $status = $request -> input('status') ?? "";

        $sign_status = $request -> input('sign_status') ?? "";

        $pageSize = $request -> input('page_size') ?? 15;

        $pageNo = $request -> input('page_no') ?? 1;

        $user_id = auth() -> user() -> id;

        $list = TransferLogic::getSellerOrderList($user_id, $sign_status, $status, $pageSize, $pageNo);

        return $this->success($list);
    }
}
